﻿using System.Web.Mvc;

using SimpleForum.Data.Models;
using SimpleForum.Data.Services;
using SimpleForum.Data.Repositories;

namespace SimpleForum.Controllers
{
    public class PostController : BaseController
    {
        private readonly IPostService PostService;
        private readonly IRepository<Rating> RatingRepository;
        private readonly IRepository<ReportReason> ReportRepository;

        public PostController(IPostService PostService, IRepository<Rating> RatingRepository, IRepository<ReportReason> ReportRepository)
        {
            this.PostService = PostService;
            this.RatingRepository = RatingRepository;
            this.ReportRepository = ReportRepository;
        }

        [Authorize, Classes.Attributes.Ajax, HttpPost]
        public ActionResult Rate(int id, string rating)
        {
            if (PostService.RatePost(id, Identity.User.Id, rating))
            {
                return new JsonResult
                {
                    ContentType = "text/plain",
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = true
                };
            }
            else return EntityNotFound();
        }

        [Authorize, ChildActionOnly]
        public ActionResult Ratings()
        {
            return PartialView(RatingRepository.Get());
        }

        [Authorize, Classes.Attributes.Ajax, HttpPost]
        public ActionResult Report(int id, string ReportReason, string Reason)
        {
            if (PostService.ReportPost(id, Identity.User.Id, ReportReason, Reason))
            {
                return new JsonResult
                {
                    ContentType = "text/plain",
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = true
                };
            }
            else return EntityNotFound();
        }

        [Authorize, ChildActionOnly]
        public ActionResult ReportReasons()
        {
            return PartialView(ReportRepository.Get());
        }
    }
}